package com.siemens.industrial.utils;

import java.io.*;

/**
 * Created by qupen on 2016/9/22.
 */
public class SiemensFileReader {
    private static String encoding = "UTF-8";

    public static String readToString(String fileName) {
        File file = new File(fileName);
        Long fileLength = file.length();
        byte[] fileContent = new byte[fileLength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(fileContent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(fileContent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return "";
        }
    }

    public static String readToString(InputStream inputStream) {
        String content = "";
        try {
            InputStreamReader read = new InputStreamReader(
                    inputStream, encoding);//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                content += lineTxt;
            }
            read.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}
